import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import cn.afterturn.easypoi.handler.inter.IWriter;
import com.cfoco.entity.Breeder;
import com.cfoco.service.BreederService;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demoDate extends BaseTest {

    @Resource
    BreederService breederService;

    @Test
    public void testimport() throws Exception{

        ImportParams params=new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setNeedVerify(true);
        //params.setNeedSave(true);
        //params.setVerifyGroup(new Class[]{});

        File file = new File("D:\\import\\breeder.xlsx");

        //List<Breeder> breeders = ExcelImportUtil.importExcel(file, Breeder.class, params);
        ExcelImportResult<Breeder> result = ExcelImportUtil.importExcelMore(file, Breeder.class, params);
        if(!result.isVerifyFail()){
            List<Breeder> breeders = result.getList();
            for (Breeder breeder : breeders) {
                breederService.add(breeder);
            }
        }else{
            FileOutputStream fos = new FileOutputStream("d:/import/error/failwork.xlsx");
            result.getFailWorkbook().write(fos);
            fos.flush();
            fos.close();
        }
    }
    
    @Test
    public void testbigDataImport() throws IOException {

        TemplateExportParams params=new TemplateExportParams();
        HashMap<String, Object> map = new HashMap<>();

        map.put("date", "2014-12-25");
        map.put("money", 2000000.00);
        map.put("upperMoney", "贰佰万");
        map.put("company", "执笔潜行科技有限公司");
        map.put("bureau", "财政局");
        map.put("person", "JueYue");
        map.put("phone", "1879740****");

        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("id", i + 1 + "");
            lm.put("zijin", i * 10000 + "");
            lm.put("bianma", "A001");
            lm.put("mingcheng", "设计");
            lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
            lm.put("quancheng", "开源项目");
            lm.put("sqje", i * 10000 + "");
            lm.put("hdje", i * 10000 + "");

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        File file = new File("D:/export/");
        if(!file.exists()){
            file.mkdirs();
        }

        FileOutputStream fos = new FileOutputStream("D:/excel/专项支出用款申请书_map.xls");
        workbook.write(fos);
        fos.close();
    }

}